package com.platform.enums;

import com.platform.comm.enums.IDictEntry;
import com.platform.comm.enums.IEnumItem;
import com.platform.comm.enums.IEnumOption;

import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 公示期限枚举
 *
 * @author lch
 */
public enum DirPublicPeriod implements IDictEntry, IEnumItem, IEnumOption<String> {

    /**
     * 三个月
     */
    THREE_MONTH("1_THREE_MONTH_M", "三个月", 3, ChronoUnit.MONTHS),

    /**
     * 半年
     */
    HALF_YEAR("2_HALF_YEAR_Y", "半年", 6, ChronoUnit.MONTHS),

    /**
     * 一年
     */
    ONE_YEAR("3_ONE_YEAR_Y", "一年", 1, ChronoUnit.YEARS),

    /**
     * 两年
     */
    TWO_YEAR("4_TWO_YEAR_Y", "两年", 2, ChronoUnit.YEARS),

    /**
     * 三年
     */
    THREE_YEAR("5_THREE_YEAR_Y", "三年", 3, ChronoUnit.YEARS),

    /**
     * 五年
     */
    FIVE_YEAR("6_FIVE_YEAR_Y", "五年", 5, ChronoUnit.YEARS),

    /**
     * 长期
     */
    LONG_TIME("7_LONG_TIME_M", "长期", 10, ChronoUnit.YEARS);

    public final String code;
    public final String title;
    public final long num;
    public final ChronoUnit unit;

    DirPublicPeriod(String code, String title, long num, ChronoUnit unit) {
        this.code = code;
        this.title = title;
        this.num = num;
        this.unit = unit;
    }

    public static DirPublicPeriod parse(String code) {
        for (DirPublicPeriod item : values()) {
            if (item.is(code)) {
                return item;
            }
        }
        return null;
    }

    public static DirPublicPeriod parseText(String text) {
        for (DirPublicPeriod item : values()) {
            if (item.isText(text)) {
                return item;
            }
        }
        return null;
    }

    @Override
    public boolean is(String code) {
        return this.code.equals(code);
    }

    private boolean isText(String text) {
        return this.title.equals(text);
    }

    @Override
    public String getKey() {
        return code;
    }

    @Override
    public String getValue() {
        return title;
    }

    @Override
    public Object getData() {
        return this;
    }


    @Override
    public String key() {
        return name();
    }

    @Override
    public String value() {
        return title;
    }

    @Override
    public Map<String, String> enums() {
        return Arrays.stream(values()).collect(Collectors.toMap(IEnumOption::key, IEnumOption::value));
    }
}
